<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


<!--      <el-form-item label="提交时间" prop="registrationDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.registrationDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择注册时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="学号" prop="identificationStuNumber">
        <el-input
          v-model="queryParams.identificationStuNumber"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="identificationCardNumber">
        <el-input
          v-model="queryParams.identificationCardNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['usermanage:rider:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['usermanage:rider:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip/>
      <el-table-column label="骑手ID" align="center" prop="riderId" show-overflow-tooltip/>
      <el-table-column label="用户ID" align="center" prop="userId" show-overflow-tooltip/>
      <el-table-column label="姓名" align="center" prop="name" show-overflow-tooltip/>
      <el-table-column label="手机号码" align="center" prop="phone" show-overflow-tooltip/>
<!--      <el-table-column label="电子邮件" align="center" prop="email" />-->

      <el-table-column label="学号" align="center" prop="identificationStuNumber" show-overflow-tooltip />
<!--      <el-table-column label="学生证" align="center" prop="identificationStuImageUrl" />-->
      <el-table-column label="学生证" align="center" prop="identificationStuImageUrl" width="100">
        <template slot-scope="scope">
          <div v-for="(imageUrl, index) in parseImageArray(scope.row.identificationStuImageUrl)" :key="index">
            <image-preview :src="imageUrl" :width="50" :height="50"/>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column label="身份证号" align="center" prop="identificationCardNumber" />-->
      <el-table-column label="身份证号" align="center" prop="identificationCardNumber" show-overflow-tooltip />

      <el-table-column label="身份证" align="center" prop="identificationCardImageUrl" width="100">
        <template slot-scope="scope">
          <div v-for="(imageUrl, index) in parseImageArray(scope.row.identificationCardImageUrl)" :key="index">
            <image-preview :src="imageUrl" :width="50" :height="50"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="registrationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="认证状态" align="center" prop="authStatus" />-->
      <el-table-column label="认证状态" align="center" prop="authStatus">
        <template slot-scope="scope">
          <el-tag :type="getStatusTagType(scope.row.authStatus)">
            {{
            (() => {
              switch (scope.row.authStatus) {
                case "审核中":
                  return '待审核';
                case "审核失败":
                  return '已拒绝';
                case "已认证":
                  return '已通过';
                default:
                  return '';
              }
            })()
            }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.authStatus !== '已认证' && scope.row.authStatus !== '审核失败'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['usermanage:rider:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleDelete(scope.row)"
            v-hasPermi="['usermanage:rider:remove']"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改骑手信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" :disabled="!editable" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" :disabled="!editable"/>
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="用户ID为空" :disabled="!editable"/>
        </el-form-item>
        <el-form-item label="信用分" prop="scores">
          <el-input v-model="form.scores" placeholder="请输入骑手信用分" :disabled="!editable"/>
        </el-form-item>
        <el-form-item label="申请时间" prop="registrationDate"　:disabled="!editable">
          <el-date-picker clearable
            v-model="form.registrationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学号" prop="identificationStuNumber">
          <el-input v-model="form.identificationStuNumber" placeholder="学号为空"　:disabled="!editable" />
        </el-form-item>
        <el-form-item label="学生证" prop="identificationStuImageUrl">
          <image-upload v-model="form.identificationStuImageUrl"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="identificationCardNumber">
          <el-input v-model="form.identificationCardNumber" placeholder="身份证号为空"　:disabled="!editable" />
        </el-form-item>
        <el-form-item label="身份证" prop="identificationCardImageUrl">
          <image-upload v-model="form.identificationCardImageUrl"　:disabled="!editable"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">通过</el-button>
        <el-button @click="handleDelete(form)">拒绝</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRider, getRider, delRider, addRider, updateRider } from "@/api/usermanage/rider";

export default {
  name: "Rider",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 骑手信息表格数据
      riderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        email: null,
        scores: null,
        registrationDate: null,
        identificationStuNumber: null,
        identificationStuImageUrl: null,
        identificationCardNumber: null,
        identificationCardImageUrl: null,
        authStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        registrationDate: [
          { required: true, message: "注册时间不能为空", trigger: "blur" }
        ],
        identificationStuNumber: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        identificationCardNumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //认证状态颜色改变
    getStatusTagType(authStatus) {
      switch (authStatus) {
        case "审核失败":
          return 'danger'; // 禁用状态，红色背景
        case "已认证":
          return 'success'; // 正常状态，绿色背景
        case "审核中":
          return 'warning'; // 注销状态，黄色背景
        default:
          return ''; // 默认为空
      }
    },
    /** 查询骑手信息列表 */
    getList() {
      this.loading = true;
      listRider(this.queryParams).then(response => {
        this.riderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    parseImageArray(imageString) {
      // 将后端返回的图片字符串转换成数组
      console.log(imageString,"12312")
      return imageString ? JSON.parse(imageString) : [];
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        riderId: null,
        userId: null,
        name: null,
        phone: null,
        email: null,
        scores: null,
        registrationDate: null,
        identificationStuNumber: null,
        identificationStuImageUrl: null,
        identificationCardNumber: null,
        identificationCardImageUrl: null,
        authStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.riderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加骑手信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const riderId = row.riderId || this.ids
      getRider(riderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改骑手信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.riderId != null) {
            updateRider(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRider(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const riderIds = row.riderId || this.ids;
      this.$modal.confirm('是否确认拒绝骑手信息编号为"' + riderIds + '"的骑手申请？').then(function() {
        return delRider(riderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("已拒绝");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('usermanage/rider/export', {
        ...this.queryParams
      }, `rider_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
